package activiti;

import java.util.List;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.User;
import org.activiti.engine.impl.persistence.entity.GroupEntity;
import org.activiti.engine.impl.persistence.entity.UserEntity;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.junit.Test;

/**
 * 创建候选组
 * @author Shea
 *
 */

public class GroupCandidate {
	/*
	 * 部署流程
	 */
	@Test
	public void deployment(){
		DeploymentBuilder deploymentBuilder = processEngine.getRepositoryService().createDeployment();
		deploymentBuilder.addClasspathResource("customer.bpmn");
		deploymentBuilder.addClasspathResource("customer.png");
		Deployment deploy = deploymentBuilder.deploy();
	}

	
	/**
	 * 候选组任务--创建组
	 */
	ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
	@Test
	public void createGroup(){
		Group group = new GroupEntity();
		group.setId("财务人员组");
		group.setName("财务人员组");
		processEngine.getIdentityService().saveGroup(group);//保存组,通过identityService
	}
	/*
	 * 创建用户
	 */
	@Test
	public void createUser(){
		User user = new UserEntity();
		user.setId("010");
		user.setFirstName("Shea");
		processEngine.getIdentityService().saveUser(user);
	}
	/*
	 * 将用户加入组中
	 */
	@Test
	public void addUser2Group(){
		String userId="010";
		String groupId="财务人员组";
		processEngine.getIdentityService().createMembership(userId, groupId);
	}
	
	/*
	 * 启动流程实例
	 */
	@Test
	public void startInstance(){
		//查询定义key
		//String defKey = processEngine.getRepositoryService().createProcessDefinitionQuery().list().get(0).getKey();
		/*ProcessDefinitionQuery definitionQuery = processEngine.getRepositoryService().createProcessDefinitionQuery();
		List<ProcessDefinition> list = definitionQuery.list();
		for (ProcessDefinition processDefinition : list) {
			System.out.println(processDefinition.getKey());
		}*/
		//System.out.println("定义id:"+defKey);
		//启动实例
		processEngine.getRuntimeService().startProcessInstanceByKey("customer");
	}
	
	/**
	 * 办理个人任务
	 */
	@Test
	public void dealTask(){
		TaskQuery taskQuery = processEngine.getTaskService().createTaskQuery();//根据登陆用户查任务
		String taskId = taskQuery.taskAssignee("赵六").list().get(0).getId();
		processEngine.getTaskService().complete(taskId);
	}
	/*
	 * 查询组任务
	 */
	@Test
	public void showGroup(){
		TaskQuery taskQuery = processEngine.getTaskService().createTaskQuery();
		//根据登陆用户查询组任务
		taskQuery.taskCandidateUser("010");//必须根据登陆人的id
		List<Task> list = taskQuery.list();
		System.out.println(list.size());
		for (Task task : list) {
			System.out.println(task.getId()+"..."+task.getName());
		}
	}
	/*
	 * 拾取组任务
	 */
	@Test
	public void getTask(){
		TaskQuery taskQuery = processEngine.getTaskService().createTaskQuery();
		taskQuery.taskCandidateUser("011");//根据登陆人的id
		List<Task> list = taskQuery.list();
		String id = list.get(0).getId();
		System.out.println(id);
		processEngine.getTaskService().claim(id, "yang");
	}
	/*
	 * 放弃组任务,让另一个人处理
	 */
	@Test
	public void dropTask(){
		processEngine.getTaskService().setAssignee("5102", null);
	}
}
